package timer;

import java.util.TimerTask;
import java.util.concurrent.*;

public class ScheduleExecutorTest {
    static ScheduledExecutorService scheduledExecutorServicePool = Executors.newScheduledThreadPool(2);
    private static long start;

    public static void main(String[] args) throws InterruptedException {
        final int id1 = 2;
        TimerTask task1 = new TimerTask() {
            @Override
            public void run() {
                try {

                    System.out.println("task1 invoked ! " + id1 +
                            +(System.currentTimeMillis() - start));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };
        final int id2 = 2;
        TimerTask task2 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task2 invoked ! " + id2 +
                        +(System.currentTimeMillis() - start));
//                    throw new NullPointerException();

            }
        };


        final int id3 = 3;
        TimerTask task3 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task3 invoked ! " + id3 +
                        +(System.currentTimeMillis() - start));
//                Thread.
            }
        };

        start = System.currentTimeMillis();
        Future future = scheduledExecutorServicePool.scheduleAtFixedRate(task1, 1000, 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorServicePool.scheduleAtFixedRate(task2, 1000, 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorServicePool.scheduleAtFixedRate(task3, 1000, 1000, TimeUnit.MILLISECONDS);
        Thread.sleep(10000);
        System.out.println("papapap");
        future.cancel(true);

        //        Thread.sleep(100000);

//        scheduledExecutorServicePool.
    }
}
